package com.example.demo.controller;


import com.example.demo.dto.AccountDto;

import com.example.demo.dto.AddCartDto;
import com.example.demo.dto.CartDto;
import com.example.demo.dto.UserShopping;
import com.example.demo.entity.User;
import com.example.demo.model.Result;
import com.example.demo.service.ShoppingCartService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author Dfrus
 * @Date 2022/2/17 20:05
 **/
@RestController
@RequestMapping("/shopping")
@AllArgsConstructor
public class ShoppingCartController {
    private ShoppingCartService service;

    @GetMapping("/{id}")
    public Result getCart(@PathVariable Integer id){
        return Result.buildSuccess(service.selectCart(id));
    }

    @PostMapping()
    public Result updateCart(@RequestBody CartDto cartDto){
        service.updateCart(cartDto);
        return Result.buildSuccess(service.findOne(cartDto));
    }

    @PostMapping("/user")
    public Result getUserID(@RequestBody UserShopping userName){
        User userId = service.findUserId(userName.getUserName());
        return Result.buildSuccess(userId);
    }

    @DeleteMapping("/{cartId}")
    public void deleteCart(@PathVariable Integer cartId){
        service.findDelete(cartId);
    }

    @PostMapping("/account")
    public Result getAccount(@RequestBody AccountDto accountDto) {
        return Result.buildSuccess(service.findAccount(accountDto.getSkuId()));
    }

    @PostMapping("/a")
    public Result addCart(@RequestBody AddCartDto addCartDto) {
        service.addCart(addCartDto);
        return Result.buildSuccess();
    }
}
